package net.yegong.util.reflect;

/**
 * The NameIdiom give one of common agreed naming style.
 * <table>
 * <tr>
 * <td>as text file content</td>
 * <td>name-like-this</td>
 * </tr>
 * <tr>
 * <td>as Java's class name</td>
 * <td>NameLikeThis</td>
 * </tr>
 * <tr>
 * <td>as Java's member field</td>
 * <td>nameLikeThis</td>
 * </tr>
 * <tr>
 * <td>as Java's getter method</td>
 * <td>getNameLikeThis or isNameLikeThis (if is a boolean field)</td>
 * </tr>
 * <tr>
 * <td>as Java's setter method</td>
 * <td>setNameLikeThis</td>
 * </tr>
 * </table>
 * @author cooper
 */
public class NamingIdiom {

	public static String toClassName(String name) {
		StringBuilder buf = new StringBuilder();
		boolean nextUpperCase = true;
		for (char ch : name.toCharArray()) {
			if (ch == '_') {
				nextUpperCase = true;
			} else if (!Character.isJavaIdentifierPart(ch)) {
				nextUpperCase = true;
			} else if (nextUpperCase) {
				nextUpperCase = false;
				buf.append(Character.toUpperCase(ch));
			} else {
				buf.append(Character.toLowerCase(ch));
			}
		}
		return buf.toString();
	}

	public static String toFieldName(String name) {
		StringBuilder buf = new StringBuilder();
		boolean nextUpperCase = false;
		for (char ch : name.toCharArray()) {
			if (ch == '_') {
				nextUpperCase = true;
			} else if (!Character.isJavaIdentifierPart(ch)) {
				nextUpperCase = true;
			} else if (nextUpperCase) {
				nextUpperCase = false;
				buf.append(Character.toUpperCase(ch));
			} else {
				buf.append(Character.toLowerCase(ch));
			}
		}
		return buf.toString();
	}

	public static String toStaticFieldName(String name) {
		StringBuilder buf = new StringBuilder();
		for (char ch : name.toCharArray()) {
			if (ch == '_') {
				buf.append('_');
			} else if (!Character.isJavaIdentifierPart(ch)) {
				buf.append('_');
			} else {
				buf.append(Character.toUpperCase(ch));
			}
		}
		return buf.toString();
	}

	public static String toSetterName(String name) {
		StringBuilder buf = new StringBuilder();
		buf.append("set");
		boolean nextUpperCase = true;
		for (char ch : name.toCharArray()) {
			if (ch == '_') {
				nextUpperCase = true;
			} else if (!Character.isJavaIdentifierPart(ch)) {
				nextUpperCase = true;
			} else if (nextUpperCase) {
				nextUpperCase = false;
				buf.append(Character.toUpperCase(ch));
			} else {
				buf.append(Character.toLowerCase(ch));
			}
		}
		return buf.toString();
	}

	public static String toGetterName(String name, boolean isBoolean) {
		StringBuilder buf = new StringBuilder();
		if (isBoolean) {
			buf.append("is");
		} else {
			buf.append("get");
		}
		boolean nextUpperCase = true;
		for (char ch : name.toCharArray()) {
			if (Character.isLetterOrDigit(ch)) {
				if (nextUpperCase) {
					nextUpperCase = false;
					buf.append(Character.toUpperCase(ch));
				} else {
					buf.append(Character.toLowerCase(ch));
				}
			} else {
				nextUpperCase = true;
			}
		}
		return buf.toString();
	}
}
